/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pua.uniandes.entities;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Juan-VM
 */
public class ServidorTransmision {

   
    private ServerSocket ss;
    private Socket connection;
    private static final int CANTIDAD_PAQUETE = 20;

    public void recibirPaqueteVideo() {
        try {
            ss = new ServerSocket(2525);

            for (int i = 0; i < CANTIDAD_PAQUETE; i++) {

                System.out.println("Waiting for connection...");
                connection = ss.accept();
                System.out.println("Connection succesful:" + connection);

            }



        } catch (IOException ex) {
            Logger.getLogger(ServidorTransmision.class.getName()).log(Level.SEVERE, null, ex);
        }



    }

    public ServidorTransmision() {

//        recibirPaqueteVideo();





    }

    public void inicializarTransmisionVideo(String address, int puertoCliente) throws UnknownHostException, InterruptedException, SocketException {

      DatagramSocket socket = new DatagramSocket((puertoCliente+1000), InetAddress.getByName("localhost"));

        for (int i = 0; i < CANTIDAD_PAQUETE; i++) {
            File f = new File("video" + i + ".wmv");
            enviarPaqueteVideo(socket,f, i,address,puertoCliente);


        }



    }

    private void enviarPaqueteVideo(DatagramSocket ds,File f, int num,String address, int port) throws InterruptedException {


        try {

            Thread.sleep(1000);

            byte[] buf = new byte[1024];
            FileInputStream in = new FileInputStream(f);
            int i = 0;
            int bytecount = 1024;
            while ((i = in.read(buf, 0, 1024)) != -1) {

                bytecount = bytecount + 1024;
                DatagramPacket dato = new DatagramPacket(buf,
                        buf.length, InetAddress.getByName("localhost"),
                        port);
                ds.send(dato);


            }
            DatagramPacket dato = new DatagramPacket("Ya termino".getBytes(),
                    "Ya termino".getBytes().length, InetAddress.getByName("localhost"),
                    port);

            ds.send(dato);
            System.out.println("se envio el paquete NO." + num);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void receiveFile(File file, InputStream is) throws IOException {
        byte[] b = new byte[1024];
        int len = 0;
        int bytcount = 1024;
        FileOutputStream inFile = new FileOutputStream(file);

        BufferedInputStream in2 = new BufferedInputStream(is, 1024);
        while ((len = in2.read(b, 0, 1024)) != -1) {
            bytcount = bytcount + 1024;
            inFile.write(b, 0, len);
        }

        System.out.println("Bytes Writen : " + bytcount);

        // Sending the response back to the client.
        ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
        oos.flush();
        oos.writeObject("ok");
        System.out.println("Message sent to the client is " + "ok");


        inFile.close();
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
